package com.example.myapplication2;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
    MediaPlayer mediaPlayer;

    public MyService() {
        Log.d("xr", "MyService Constructor...");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("xr", "MyService onCreate...");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("xr", "MyService onStartCommand...");
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d("xr", "MyService onBind...");
        return new MyBinder();
    }

    @Override
    public void onDestroy() {
        Log.d("xr", "MyService onDestroy...");
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
        mediaPlayer = null;
        super.onDestroy();
    }

    public class MyBinder extends Binder {
        public void todo() {
            Log.d("xr", "MyService MyBinder.todo()...");
            if (mediaPlayer == null) {
                mediaPlayer = MediaPlayer.create(MyService.this, R.raw.wzyo);
            }
            if (!mediaPlayer.isPlaying()) {
                mediaPlayer.start();
            }
        }
    }
}
